package com.team6.stickynote.ui;

import android.util.Log;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.team6.stickynote.App;
import com.team6.stickynote.beans.StickyNoteBean;
import com.team6.stickynote.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

public class SearchViewModel extends ViewModel {

    public SearchViewModel() {
        updateRepo();
    }

    private static final String TAG = "SearchViewModel";

    private List<StickyNoteBean> repoStickyNoteBeans;

    private final MutableLiveData<List<StickyNoteBean>> _stickyNoteBeans = new MutableLiveData<>();
    LiveData<List<StickyNoteBean>> stickyNoteBeans = _stickyNoteBeans;

    public void updateRepo() {
        repoStickyNoteBeans = App.database.queryAllData();
    }

    public void search(String keyword) {
        List<StickyNoteBean> searchResult = new ArrayList<>();
        for (int i = 0; i < repoStickyNoteBeans.size(); i++) {
            StickyNoteBean stickyNoteBean = repoStickyNoteBeans.get(i);
            Log.d(TAG, "i = " + i + ", title = " + stickyNoteBean.getTitle() + ", text = " + stickyNoteBean.getText());
            if (stickyNoteBean.getTitle().contains(keyword) || stickyNoteBean.getText().contains(keyword)) {
                searchResult.add(stickyNoteBean);
            }
        }
        _stickyNoteBeans.setValue(searchResult);
    }

}
